package com.castle.fortress.admin.pay.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.pay.dto.PaySceneDto;
import com.castle.fortress.admin.pay.entity.PaySceneEntity;
import com.castle.fortress.admin.pay.service.PaySceneService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 支付场景表 api 控制器
 *
 * @author castle
 * @since 2022-07-07
 */
@Api(tags="支付场景表api管理控制器")
@RestController
@RequestMapping("/api/pay/payScene")
public class ApiPaySceneController {
    @Autowired
    private PaySceneService paySceneService;


    /**
     * 支付场景表的分页展示
     * @param paySceneDto 支付场景表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("支付场景表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<PaySceneDto>> pagePayScene(PaySceneDto paySceneDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<PaySceneDto> page = new Page(pageIndex, pageSize);

        IPage<PaySceneDto> pages = paySceneService.pagePayScene(page, paySceneDto);
        return RespBody.data(pages);
    }

    /**
     * 支付场景表保存
     * @param paySceneDto 支付场景表实体类
     * @return
     */
    @ApiOperation("支付场景表保存")
    @PostMapping("/save")
    public RespBody<String> savePayScene(@RequestBody PaySceneDto paySceneDto){
        if(paySceneDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            PaySceneEntity paySceneEntity = ConvertUtil.transformObj(paySceneDto,PaySceneEntity.class);
        if(paySceneService.save(paySceneEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 支付场景表编辑
     * @param paySceneDto 支付场景表实体类
     * @return
     */
    @ApiOperation("支付场景表编辑")
    @PostMapping("/edit")
    public RespBody<String> updatePayScene(@RequestBody PaySceneDto paySceneDto){
        if(paySceneDto == null || paySceneDto.getId() == null || paySceneDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            PaySceneEntity paySceneEntity = ConvertUtil.transformObj(paySceneDto,PaySceneEntity.class);
        if(paySceneService.updateById(paySceneEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 支付场景表删除
     * @param ids 支付场景表id集合
     * @return
     */
    @ApiOperation("支付场景表删除")
    @PostMapping("/delete")
    public RespBody<String> deletePayScene(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(paySceneService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 支付场景表详情
     * @param id 支付场景表id
     * @return
     */
    @ApiOperation("支付场景表详情")
    @GetMapping("/info")
    public RespBody<PaySceneDto> infoPayScene(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            PaySceneEntity paySceneEntity = paySceneService.getById(id);
            PaySceneDto paySceneDto = ConvertUtil.transformObj(paySceneEntity,PaySceneDto.class);
        return RespBody.data(paySceneDto);
    }


}
